// pages/physical-package-detail/physical-package-detail.js
Page({
	data: {
		patientName: '',
		selectedPackage: null,
		selectedDate: '',
		dateList: [],
		packageDetails: [],
		totalPrice: '0.00'
	},

	onLoad(options) {
		console.log('套餐详情页面加载')
		console.log('页面参数:', options)
		
		if (options.patient) {
			this.setData({
				patientName: options.patient
			})
		}
		
		if (options.package) {
			try {
				const packageData = JSON.parse(options.package)
				this.setData({
					selectedPackage: packageData
				})
				this.loadPackageDetails(packageData)
			} catch (e) {
				console.error('解析套餐数据失败:', e)
			}
		}
		
		// 生成日期列表
		this.generateDateList()
	},

	// 生成日期列表
	generateDateList() {
		const dateList = []
		const weekdays = ['今日', '周六', '周日', '周一', '周二', '周三', '周四']
		const days = [3, 4, 5, 6, 7, 8, 9]
		
		for (let i = 0; i < 7; i++) {
			dateList.push({
				date: `2025-01-${days[i]}`,
				weekday: weekdays[i],
				day: days[i]
			})
		}
		
		this.setData({
			dateList: dateList
		})
		
		// 确保默认选中第一个日期（今日）
		if (dateList.length > 0) {
			this.setData({
				selectedDate: dateList[0].date
			})
		}
	},

	// 选择日期
	selectDate(e) {
		const date = e.currentTarget.dataset.date
		
		this.setData({
			selectedDate: date
		})
	},

	// 加载套餐详情
	loadPackageDetails(packageData) {
		// 根据套餐类型生成详情列表
		const details = this.getPackageDetails(packageData.id)
		const totalPrice = details.reduce((sum, item) => sum + parseFloat(item.price), 0).toFixed(2)
		
		this.setData({
			packageDetails: details,
			totalPrice: totalPrice
		})
	},

	// 获取套餐详情数据
	getPackageDetails(packageId) {
		const detailsMap = {
			'male_basic_a': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' }
			],
			'male_basic_b': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' }
			],
			'male_comprehensive': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' }
			],
			'male_premium': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' }
			],
			'male_vip': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' },
				{ id: 9, name: 'CT', price: '100.00' }
			],
			'female_basic_a': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '肝功能常规', price: '100.00' },
				{ id: 7, name: '肾功能常规', price: '100.00' }
			],
			'female_basic_b': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '肝功能常规', price: '100.00' },
				{ id: 8, name: '肾功能常规', price: '100.00' }
			],
			'female_comprehensive': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: '肝功能常规', price: '100.00' },
				{ id: 9, name: '肾功能常规', price: '100.00' }
			],
			'female_premium': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' },
				{ id: 9, name: '肝功能常规', price: '100.00' },
				{ id: 10, name: '肾功能常规', price: '100.00' }
			],
			'female_vip': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' },
				{ id: 9, name: 'CT', price: '100.00' },
				{ id: 10, name: '肝功能常规', price: '100.00' },
				{ id: 11, name: '肾功能常规', price: '100.00' }
			],
			'elderly_basic': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' }
			],
			'elderly_comprehensive': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' }
			],
			'elderly_premium': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' },
				{ id: 9, name: 'CT', price: '100.00' },
				{ id: 10, name: '肝功能常规', price: '100.00' }
			],
			'onboarding_basic': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' }
			],
			'onboarding_comprehensive': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' }
			],
			'onboarding_premium': [
				{ id: 1, name: '一般检查', price: '100.00' },
				{ id: 2, name: '内科检查', price: '100.00' },
				{ id: 3, name: '外科检查', price: '100.00' },
				{ id: 4, name: '血常规', price: '100.00' },
				{ id: 5, name: '尿常规', price: '100.00' },
				{ id: 6, name: '心电图', price: '100.00' },
				{ id: 7, name: '胸片', price: '100.00' },
				{ id: 8, name: 'B超', price: '100.00' },
				{ id: 9, name: '肝功能常规', price: '100.00' }
			]
		}
		
		return detailsMap[packageId] || [
			{ id: 1, name: '一般检查', price: '100.00' },
			{ id: 2, name: '内科检查', price: '100.00' },
			{ id: 3, name: '外科检查', price: '100.00' },
			{ id: 4, name: '血常规', price: '100.00' },
			{ id: 5, name: '尿常规', price: '100.00' },
			{ id: 6, name: '肝功能常规', price: '100.00' },
			{ id: 7, name: '肾功能常规', price: '100.00' }
		]
	},

	// 下一步
	nextStep() {
		console.log('点击下一步')
		console.log('选择的套餐:', this.data.selectedPackage)
		console.log('选择的日期:', this.data.selectedDate)
		console.log('总价格:', this.data.totalPrice)
		
		// 跳转到确认支付页面
		wx.navigateTo({
			url: '/pages/physical-confirm/physical-confirm?patient=' + this.data.patientName + 
				 '&package=' + JSON.stringify(this.data.selectedPackage) + 
				 '&date=' + this.data.selectedDate + 
				 '&totalPrice=' + this.data.totalPrice
		})
	},

	// 切换就诊人
	switchPatient() {
		console.log('切换就诊人')
		wx.navigateTo({
			url: '/pages/patient-select-physical/patient-select-physical'
		})
	}
})
